package ua.at.tsvetkov.samizdat.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Layout;
import android.text.Spanned;
import android.view.Display;
import android.view.View;
import android.widget.TextView;

public class AdtTextFlipper extends PagerAdapter implements OnPageChangeListener {

	private List<View>				views					= null;
	private int							currentPosition	= 0;
	private Activity					activity;

	int									screenWidth;
	int									screenHeight;

	private Spanned					text;
	private Spanned					textTmp;
	private ArrayList<Integer[]>	pages					= new ArrayList<Integer[]>();

	public AdtTextFlipper(List<View> views, Spanned text, Activity activity) {
		Display display = activity.getWindowManager().getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = 1097;
		this.text = text;
		this.views = views;
		this.activity = activity;
	}

	@Override
	public Object instantiateItem(View collection, int position) {
		View v = views.get(position);
		((ViewPager) collection).addView(v, 0);
		return v;
	}

	@Override
	public void destroyItem(View collection, int position, Object view) {
		((ViewPager) collection).removeView((View) view);
	}

	@Override
	public int getCount() {
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {

	}

	@Override
	public void onPageSelected(int position) {
		final TextView currentTextView = (TextView) views.get(position);

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (currentTextView.getPaint() == null) {
					try {
						Thread.sleep(400);
					} catch (Exception e) {
					}
				}
				Layout textViewLayout = currentTextView.getLayout();
				Paint paint = currentTextView.getPaint();
				final int maxLineCount = (screenHeight) / currentTextView.getLineHeight();

				textTmp = text;

				final int aa = paint.breakText(textTmp.toString(), true, 800, null);

				activity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						currentTextView.setText(textTmp.subSequence(0, aa*20));
					}
				});
				// while (textTmp.length() != 0) {
				// int start = textViewLayout.getLineStart(0);
				// int end = textViewLayout.getLineEnd(maxLineCount - 1);
				// Integer[] startEnd = new Integer[2];
				// startEnd[0] = start;
				// startEnd[1] = end;
				//
				// pages.add(startEnd);
				//
				// textTmp = (Spanned) textTmp.subSequence(end, textTmp.length());
				// }
			}
		}).start();
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}
}
